package com.hq.common.utils;

import javax.sql.rowset.serial.SQLOutputImpl;
import java.io.CharArrayReader;
import java.util.regex.Matcher;

public class StringUtil {
    private static final String CODE="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    private static final String NUM="0123456789";
    //判断源字符串是否有值，空引号也算没值
    public static  boolean hasText(String string){
       return !(string==null||"".equals(string));
    }

    //判断源字符串是否有值，空引号和空格也算没值
    public static  boolean hasTextAndSpace(String string){
        return !(string==null||"".equals(string.trim()));
    }

    //判断是否为手机号码
    //"^1(3\\d{2}|4[14-9]\\d|5([0-35689]\\d|7[1-79])|66\\d|7[2-35-8]\\d|8\\d{2}|9[13589]\\d)\\d{7}$"
    public static boolean isPhone(String phone){
        boolean matches=false;
        if(hasTextAndSpace(phone)){
            String regx="^1(3\\d{2}|4[14-9]\\d|5([0-35689]\\d|7[1-79])|66\\d|7[2-35-8]\\d|8\\d{2}|9[13589]\\d)\\d{7}$";
            matches = phone.matches(regx);
        }
        return matches;
    }

    //判断是否为电子邮箱
    public static boolean isEmail(String email){
        String regx= "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        boolean matches=false;
        if(hasTextAndSpace(email)){
            matches = email.matches(regx);
        }
        return matches;
    }


    //判断是否全部为字母
    public static boolean isLetter(String string){
        String regx="^[A-Za-z]+$";
        boolean matches=false;
        if(hasTextAndSpace(string)){
            matches = string.matches(regx);
        }
        return matches;
    }


    // 获取n位随机英文字符串
    public static String getLetter(int n){
        //  ?随机数 成为字母 然后拼接字符串   ascII 码
        //最大值  ~最小值随机数  int(Math.random()*(最大值-最小值)+最小值)
        //0~CODE.length();
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<n;i++){
            int length=CODE.length();
            int index=(int)((Math.random()*length));
            char c = CODE.charAt(index);
            stringBuffer.append(c);
        }
        return stringBuffer.toString();
    }

//    获取n位随机英文和数字字符串
    public static String getLetterAndNum(int n){
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<n;i++){
            int length=(CODE+NUM).length();
            int index=(int)((Math.random()*length));
            char c = (CODE+NUM).charAt(index);
            stringBuffer.append(c);
        }
        return stringBuffer.toString();
    }


    //获取n个随机中文字符串
    public  static  String getChina(int n){
        //4e00~9fa5  16进制   int(Math.random()*(最大值-最小值)+最小值)
        int min = Integer.parseInt("4e00", 16);
        int max = Integer.parseInt("9fa5", 16);
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<n;i++){
            int index=(int)((Math.random()*(max-min+1)+min));
            stringBuffer.append((char)index);
        }
        return stringBuffer.toString();

    }

}



